<html>
<head>
    <title>WEBSQL-SQL执行计划新增</title>
    <script src="/static/statics/libs/jquery-1.7.2.js"></script>
    <link rel="stylesheet" href="/static/statics/css/font-awesome.min.css">
    <link rel="stylesheet" href="/static/statics/plugins/layui/css/layui.css" media="all">
    <script src="/static/statics/plugins/layer/layer.js"></script>
    <script src="/static/statics/plugins/layui/layui.js"></script>
    <link rel="stylesheet" href="/static/common/css/cyStyle.css">
    <link rel="stylesheet" href="/static/common/css/cyType.css">
    <link rel="stylesheet" href="/static/code/codemirror.css">
    <link rel="stylesheet" href="/static/code/show-hint.css"/>
    <script src="/static/common/js/pageGrid1.js"></script>
    <script src="/static/common/js/editGrid.js"></script>
    <script src="/static/common/js/whole/cyLayer.js"></script>
    <script src="/static/common/js/whole/common.js"></script>
    <script src="/static/common/js/selectTool.js"></script>
    <script src="/static/code/codemirror.js"></script>
    <script src="/static/code/matchbrackets.js"></script>
    <script src="/static/code/sql.js"></script>
    <script src="/static/code/show-hint.js"></script>
    <script src="/static/code/sql-hint.js"></script>
    <style>
        .sqltxt {
            border: 1px solid;
            border-radius: 12px;
            box-shadow: 2px 2px 5px #bbb;
            font-size: 20px;
            height: 600px;
        }

        .sqlTab {
            border: 1px solid;
            border-radius: 12px;
            box-shadow: 2px 2px 5px #bbb;
            font-size: 14px;
            margin: 12px;
            position: relative;
        }

        .menus {
            border: 1px solid;
            border-radius: 12px;
            box-shadow: 2px 2px 5px #bbb;
            margin: 5px;
        }

        .layui-form-item {
            margin-bottom: 5px;
            margin-top: 5px;
        }
    </style>
</head>
<body>
<div class="menus" style="margin:5px;">
    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">作业数据源:</label>
            <div class="layui-input-inline">
                <div cyType="selectTool"
                     cyProps="url:'/dataSourceManager/findDataSourceList',search:'true',filter:'dbtype'"
                     name="timingName" lay-search id="timingName" value="0" cyProps.tips="请选择作业数据源"
                     class="layui-input-inline"></div>
            </div>
            <label class="layui-form-label">同步数据源:</label>
            <div class="layui-input-inline">
                <div cyType="selectTool"
                     cyProps="url:'/dataSourceManager/findDataSourceList',search:'true',filter:'dbtype'"
                     name="syncName" lay-search id="syncName" value="0" cyProps.tips="请选择同步数据源"
                     class="layui-input-inline"></div>
            </div>
            <label class="layui-form-label">同步表名:</label>
            <div class="layui-input-inline" style="width: 150px;">
                <input type="text" name="syncTable" id="syncTable" class="layui-input" placeholder="同步数据源表名"/>
            </div>
            <label class="layui-form-label">作业名称:</label>
            <div class="layui-input-inline" style="width: 120px;">
                <input type="text" id="col2" class="layui-input"/>
            </div>
            <label class="layui-form-label">CRON:</label>
            <div class="layui-input-inline" style="width: 120px;">
                <input type="text" id="executeTimeHidden" class="layui-input"/>
            </div>
            <div class="">
                <button class="layui-btn search-btn" id="queryBtn">
                    <i class="fa fa-save">&nbsp;</i>保存
                </button>

                <button class="layui-btn layui-btn-warm"
                        onclick="openIframe('在线生成Cron后 复制Cron 表达式','http://cron.qqe2.com/',3001);return false">
                    <i class="fa fa-link">&nbsp;</i> CRON
                </button>
            </div>
        </div>
    </form>
</div>

<div class="sqltxt">
    <textarea id='sqlText' style="width: 100%;height: 600px;display:block;"></textarea>
</div>

<script>
    layui.use(['form', 'table', 'element'], function () {
        form = layui.form;
        table = layui.table;
        element = layui.element;
        var source = {app: [], version: []};
        editor = CodeMirror.fromTextArea(document.getElementById("sqlText"), {
            lineNumbers: true,
            mode: {
                name: "text/x-plsql"
            },
            matchBrackets: true,
            styleActiveLine: true, // 当前行背景高亮
            indentWithTabs: true,
            smartIndent: true,
            lineNumbers: true,
            spellcheck: true,
            extraKeys: {
                'Ctrl': 'autocomplete',
                Tab: function (cm) {
                    var spaces = Array(cm.getOption('indentUnit') + 1).join(' ');
                    cm.replaceSelection(spaces);
                }
            },
            hintOptions: {
                tables: source,
                completeSingle: false
            }
        });
        editor.setSize('auto', '600px');
        //执行sql
        $("#queryBtn").click(function () {
            var timingName = $("#timingName  option:selected").text();
            var syncName = $("#syncName  option:selected").text();
            var executeTime = $("#executeTimeHidden").val();
            var syncTable = $("#syncTable").val();
            var col2 = $("#col2").val();
            var sqltext = editor.getValue().trim();
            if (timingName == "请选择" || timingName == '') {
                Msg.error("请先选择执行作业的数据源~");
                return false;
            }
            if (sqltext == "" || sqltext == null) {
                Msg.error("请输入要执行的SQL~");
                return false;
            }
            if (col2 == "" || col2 == null) {
                Msg.error("请输入作业名称~");
                return false;
            }
            if (executeTime == "" || executeTime == null) {
                Msg.error("请输入执行时间~");
                return false;
            }
            if (syncName == "请选择") {
                syncName = "";
            }
            if (syncTable != "") {
                if (syncName == "") {
                    Msg.error("请先选择同步数据源~");
                    return false;
                }
            }
            indexOpen = Loading.open(0, true);
            var url = "/timingManager/addTimingData";
            var params = {
                'timingName': timingName,
                'syncName': syncName,
                'executeTime': executeTime,
                'sqlText': sqltext,
                'col1': '休眠',
                'col2': col2,
                'syncTable': syncTable
            };
            $.ajax({
                type: 'POST',
                url: url,
                data: params,
                async: true,
                cache: false,
                success: function (data) {
                    Loading.close(indexOpen);
                    if (data.code == 200) {
                        Msg.success(data.msg);
                    } else {
                        Msg.error(data.msg);
                    }

                },
                error: function (data) {
                    Loading.close(indexOpen);
                    Msg.error("执行异常!");
                }
            });
            return false;
        });
    });
</script>
</body>
</html>